#ifdef a
#include"Sqlist.h"
//链表的创建
Node* BuySqlistNode(TYPE x) {
	Node* new_node = (Node*)malloc(sizeof(Node));
	if (!new_node) {
		perror("申请空间失败!");
		return NULL;
	}
	new_node->date = x;
	new_node->next = NULL;
	return new_node;
}

//尾插
void SqlistPushBack(Node** qqlist, TYPE x) {
	//qlist是第一个节点，而qqlist就是链表的地址，必须存在
	assert(qqlist);
	Node* new_node = BuySqlistNode(x);
	//链表为空(此时改变了qlist本身的值，所以需要二级指针）
	if (!*qqlist)
		*qqlist = new_node;
	//链表不为空
	else {
		Node* cur = *qqlist;
		while (cur->next) 
			cur = cur->next;
		cur->next = new_node;
	}
	return;
}

//尾删
void SqlistPopBack(Node** qqlist) {
	assert(qqlist);
	//空
	if (!*qqlist)
		return;
	//只有一个节点
	if (!((*qqlist)->next)) {
		//注意优先级('->'  >  '*')
		free(*qqlist);
		qqlist = NULL;
	}
	//多个节点(保存前一个节点)
	Node* prev = NULL;
	Node* cur = *qqlist;
	while (cur->next) {
		prev = cur;
		cur = cur->next;
	}
	//出来while循环后，cur是最后一个节点，prev是倒数第二个节点
	free(cur);
	prev->next = NULL;
	cur = NULL;
}

//头插
void SqlistPushHead(Node** qqlist,TYPE x) {
	assert(qqlist);
	Node* new_node = BuySqlistNode(x);
	if (!*qqlist)
		*qqlist = new_node;
	else {
		new_node->next = *qqlist;
		*qqlist= new_node;
	}
	return;
}

//头删
void SqlistPopHead(Node** qqlist) {
	assert(qqlist);
	//空->不需要处理
	if (!*qqlist)
		return;
	//非空
	else {
		Node* del_node = *qqlist;
		*qqlist = (*qqlist)->next;
		free(del_node);
		del_node = NULL;
	}
	return;

}

//pos后插入
void SqlistInsertAfter(Node* pos, TYPE x) {
	//这里传入的plist就是pos位置的节点，不能为空
	assert(pos);
	Node* new_node = BuySqlistNode(x);
	new_node->next = pos->next;
	pos->next = new_node;
	return;
}

//pos后删除
void SqlistEraseAfter(Node* pos) {
	//当pos为空或pos为最后一个节点时，不操作
	if (!pos || !pos->next)
		return;
	Node* del_node = pos->next;
	pos->next = del_node->next;
	free(del_node);
	del_node = NULL;
	return;
}

//查找
Node* SqlistFind(Node* plist, TYPE x) {
	//空
	if (!plist)
		return NULL;
	//非空
	Node* cur = plist;
	while (cur) {
		if (cur->date == x)
			return cur;
		cur = cur->next;
	}
	return NULL;
}

//链表的打印
void PrintSqlist(Node* qlist) {
	Node* cur = qlist;
	while (cur) {
		printf("%d->", cur->date);
		cur = cur->next;
	}
	printf("NULL\n");
	return;
}

//链表的销毁
void SqlistDestory(Node** qqlist) {
	assert(qqlist);
	//只要第一个节点时空的就不需要销毁
	//因为第一个节点是在栈上创建的，会在后面自动释放
	if (!*qqlist)
		return;
	//非空
	Node* cur = *qqlist;
	while (cur) {
		*qqlist = cur->next;
		free(cur);
		cur = *qqlist;
	}
	//最后的cur指向一定为空且已经释放完毕
	return;
}

//----------------------------------------------------------------------------------------------------------

//测试
void TestSqlist() {
	Node* qlist = NULL;
	//尾插
	SqlistPushBack(&qlist, 1);
	SqlistPushBack(&qlist, 2);
	SqlistPushBack(&qlist, 3);
	SqlistPushBack(&qlist, 4);
	PrintSqlist(qlist);
	//头插
	SqlistPushHead(&qlist, 5);
	SqlistPushHead(&qlist, 6);
	SqlistPushHead(&qlist, 7);
	PrintSqlist(qlist);
	//头删
	SqlistPopHead(&qlist);
	SqlistPopHead(&qlist);
	PrintSqlist(qlist);
	//尾删
	SqlistPopBack(&qlist);
	SqlistPopBack(&qlist);	
	PrintSqlist(qlist);
	
	//pos后插入
	SqlistInsertAfter(SqlistFind(qlist, 5), 10);
	SqlistInsertAfter(SqlistFind(qlist, 1), 11);
	SqlistInsertAfter(SqlistFind(qlist, 2), 12);
	PrintSqlist(qlist);

	//pos后删除
	SqlistEraseAfter(SqlistFind(qlist, 5));
	SqlistEraseAfter(SqlistFind(qlist, 1));
	SqlistEraseAfter(SqlistFind(qlist, 2));
	PrintSqlist(qlist);


	
	//查找
	TYPE find;
	printf("请输入要查找的元素>>");
	scanf("%d", &find);
	Node* ret = SqlistFind(qlist, find);
	if (ret)
		printf("%d is in Sqlist\n", find);
	else
		printf("%d is not in Sqlist\n", find);

		SqlistDestory(&qlist);
}
#endif


#ifdef a

#include"Sqlist.h"

int main() {
	TestSqlist();
	return 0;
}
#endif